package xm.control.bar.base
{
	import flash.display.DisplayObject;
	
	import xm.core.XMSprite;
	import xm.interfaces.ILifeCycle;
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class ContentBase implements ILifeCycle
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		/**
		 * paddingTop
		 */		
		public var paddingTop : Number = 0;
		
		/**
		 * paddingRight
		 */	
		public var paddingRight : Number = 0;
		
		/**
		 * paddingBottom
		 */	
		public var paddingBottom : Number = 0;
		
		/**
		 * paddingLeft
		 */	
		public var paddingLeft : Number = 0;
		
		/**
		 * contentList
		 */	
		public var contentList : Array = [];
		
		/**
		 * gapBetweenContents
		 */	
		public var gapBetweenContents : Number = 0;
		
		/**
		 * 화면에 add 되었는지 여부
		 * <br />
		 * <br />
		 * isAdded
		 */	
		public var isAdded : Boolean = false;
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		protected var container : BarBase;
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function ContentBase()
		{
			
		}
		
		
		/**
		 * @param content 리스트 맨 뒤에 아이템 삽입
		 * @param fn
		 */		
		public function push( content : Object ):void
		{
			contentList.push( content );
		}
		
		/**
		 * @param content
		 * @param fn
		 * @param index 해당 인덱스에 아이템 삽입
		 */		
		public function pushAt( content : Object, index:int ):void
		{
			index = Math.max( contentList.length, index );
			contentList.splice( index, 0, content );
		}
		
		/**
		 * @param index 해당 아이템제거
		 */		
		public function removeAt( index:int ):void
		{
			if( contentList.length > index ) 
			{
				var content : Object = contentList[ index ];
				contentList.splice( index, 1 );
				content = null;
			}
		}
		
		/**
		 * 리스트 앞요소 제거
		 */		
		public function shift():void
		{
			if( contentList.length > 0 ) 
			{
				delete contentList.shift();
			}
		}
		
		/**
		 * 리스트 마지막요소 제거
		 */		
		public function pop():void
		{
			if( contentList.length > 0 ) 
			{
				delete contentList.pop();
			}
		}
		
		/**
		 * Bar 에 의해 적용시 일어나는 메서드
		 */		
		public function apply( container:BarBase ):void
		{
			this.container = container;
		}
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * 변수, 객체 생성됨.
		 */		
		public function createChildren():void
		{
			
		}
		
		/**
		 * 생성완료시점. ( 화면에 처음 붙을때 발생 )
		 */		
		public function creationComplete():void
		{
			
		}
		
		/**
		 * 화면에 add됨.
		 */		
		public function add():void
		{
			isAdded = true;
			
			var len:int = contentList.length;
			for (var i:int = 0; i < len; i++) 
			{
				var content : DisplayObject = contentList[ i ] as DisplayObject;
				if( content )
				{
					container.addChild( contentList[ i ] as DisplayObject );
				}
			}
		}
		
		/**
		 * 화면에서 remove됨. 
		 */		
		public function remove():void
		{
			isAdded = false;
			
			var len:int = contentList.length;
			for (var i:int = 0; i < len; i++) 
			{
				var content : DisplayObject = contentList[ i ] as DisplayObject;
				if( content && container.contains( content ) )
				{
					container.removeChild( contentList[ i ] as DisplayObject );
				}
			}
		}
		
		/**
		 * 메모리에서 destory됨. 
		 */		
		public function destroy():void
		{
			var i:int, len:int;
			
			padding = [NaN,NaN,NaN,NaN];
			gapBetweenContents = NaN;
			isAdded = false;
			
			
			i = 0;
			len = contentList.length;
			while( i<len )
			{
				if( contentList[ i ] is ILifeCycle )
				{
					( contentList[ i ] as ILifeCycle ).destroy();
				}
				else
				{
					if( contentList[ i ] is DisplayObject )
					{
						if( ( contentList[ i ] as DisplayObject ).parent ) ( contentList[ i ] as DisplayObject ).parent.removeChild( ( contentList[ i ] as DisplayObject ) );
					}
				}
				
				delete contentList[ i ];
				++i;
			}
			
			
			container = null;
		}
		
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		/**
		 * @param arr : Array - 위, 오른쪽, 아래, 왼쪽 순서 패딩
		 */		
		public function set padding( arr:  Array ):void
		{
			this.paddingTop = arr[ 0 ];
			this.paddingRight = arr[ 1 ];
			this.paddingBottom = arr[ 2 ];
			this.paddingLeft = arr[ 3 ];
		}
		
		
		/**
		 * @return 위, 오른쪽, 아래, 왼쪽 순서 패딩
		 */		
		public function get padding(): Array
		{
			return [ paddingTop, paddingRight, paddingBottom, paddingLeft ];
		}
		
		
		/**
		 * @return Content : Array 반환
		 */		
		public function get list():Array
		{
			return contentList;
		}
		
		
		/**
		 * @return 
		 */		
		public function get width():Number
		{
			var i:int, len:int, disObj : DisplayObject, min : Number, max : Number;
			
			i = 1;
			len = contentList.length;
			
			if( len == 0 ) return 0;
			
			disObj = contentList[ 0 ] as DisplayObject;
			min = disObj.x;
			max = disObj.x + disObj.width;
			
			// 우선 컨텐츠
			while( i<len )
			{
				disObj = contentList[ i ] as DisplayObject;
				min = Math.min( min , disObj.x );
				max = Math.max( max, disObj.x + disObj.width );
				
				++i;
			}
			
			return paddingLeft+(max-min)+paddingRight;
			
		}
		
		
		/**
		 * @return 
		 */		
		public function get height():Number
		{
			var i:int, len:int, disObj : DisplayObject, min : Number, max : Number;
			
			min = 0;
			max = 0;
			i = 0;
			len = contentList.length;
			
			if( len == 0 ) return 0;
			
			// 우선 컨텐츠
			while( i<len )
			{
				disObj = contentList[ i ] as DisplayObject;
				min = Math.min( min , disObj.y );
				max = Math.max( max, disObj.y + disObj.height );
				
				++i;
			}
			
			return paddingTop + (max-min) + paddingBottom;
		}
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
	}
}